﻿//获取m~n的随机数
function get_range_num(m, n) {
    var num = Math.floor(Math.random() * (m - n) + n);
    //console.log(num);
    return num;
}

function get_rand_color() {
    var i = 0;
    var str = "#";
    var random = 0;
    var aryNum = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];

    for (i = 0; i < 6; i++) {
        random = parseInt(Math.random() * 16);
        str += aryNum[random];
    }
    return str;
}

var page = {

    /* 此方法在第一次显示窗体前发生 */
    onLoad: function (event) {

    },

    /* 此方法展示窗体后发生 */
    onResume: function (event) {

    },

    /* 当前页状态变化为显示时触发 */
    onShow: function (event) {

    },

    /* 当前页状态变化为隐藏时触发 */
    onHide: function (event) {

    },

    /* 此方法关闭窗体前发生 */
    onExit: function (event) {
    },

    btn_change: function (event) {
        pm.navigateBack();
    },

    add_data: 10,
    sub_data: 5,
    btn_again: function (event) {
        var that = this;
        var base = 100;
        var point_arr = [];

        for (var i = 0; i < 90; i++) {
            var point = {};

            point.x = i * 5;
            point.y = get_range_num(base - this.sub_data, base + this.add_data);

            if (0 == i % 10) {
                point.y = get_range_num(base + this.sub_data, base + this.add_data);//30-40
            }
            if (0 == i % (10 + 1)) {
                point.y = get_range_num(base - this.add_data, base - this.sub_data);//10-20
            }
            point_arr.push(point);
        }

        function move(pro) {
            var ctx = pm.createCanvasContext('Canvas1', that);
            pro *= 100;
            ctx.setFillStyle(get_rand_color());
            ctx.moveTo(0, 100);
            for (var i = 0; i < pro; i++) {
                if (i < point_arr.length) {
                    ctx.lineTo(point_arr[i].x, point_arr[i].y);
                }
            }
            ctx.stroke();
            ctx.draw();
        }

        var start = null;
        //动画渲染调用
        function renderCallback(timestamp) {
            if (!start)
                start = timestamp;
            var progress = (timestamp - start) / 4000;
            if (progress <= 0.999) {
                move(progress);
            }

            if (0.999 < progress) {
                start = timestamp;
                move(progress);
            }

            if (progress < 1) {
                requestAnimationFrame(renderCallback);
            }
        }
        requestAnimationFrame(renderCallback); // start first frame    
    },

    btn_add: function (event) {
        this.add_data += 20;
        this.sub_data -= 10;
    },

    btn_sub: function (event) {
        if (this.add_data <= 0) {
            console.log("slow！！");
            this.add_data = 0;
            this.sub_data = 0;
        } else {
            this.add_data -= 20;
            this.sub_data += 10;
        }
    },
};

Page(page);

page = 0;
